//
//  IDOLeadingPageController.m
//  IDOLeadingPageController
//
//  Created by 卓勇 on 2017/6/1.
//  Copyright © 2017年 zhuoyong. All rights reserved.
//

/**
 *  说明：
 *  感谢ZeroJ大神
 *  本Demo出自ZeroJ的《iOS自定义控件剖析》
 *  ZeroJ简书地址：http://www.jianshu.com/p/510500f3aebd
 *  ZeroJ源码地址：https://github.com/jasnig/ZJLeadingPageController.git
 *  本Demo地址：https://git.oschina.net/idozhuoyong/IDOLeadingPageController.git
 *
 */

#import "IDOLeadingPageController.h"

@interface IDOLeadingPageController () <UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger numberOfPages;

@property (nonatomic, copy) IDOLeadingPageSetupCellHandler setupCellHandler;
@property (nonatomic, copy) IDOLeadingPageFinishHandler finishHandler;

@end

static NSString *const KIDOLeadingPageCellId = @"KIDOLeadingPageCellId";

@implementation IDOLeadingPageController

#pragma mark - init
- (instancetype)initWithNumberOfPages:(NSInteger)numberOfPages setupCellHandler:(IDOLeadingPageSetupCellHandler)setupCellHandler finishHandler:(IDOLeadingPageFinishHandler)finishHandler {
    self = [super init];
    
    if (self) {
        self.numberOfPages = numberOfPages;
        self.setupCellHandler = setupCellHandler;
        self.finishHandler = finishHandler;
        
        // 添加collectionView
        [self.view addSubview:self.collectionView];
        // 添加pageControl
        [self.view addSubview:self.pageControl];
        // 注册cell
        [self.collectionView registerClass:[IDOLeadingPageCell class] forCellWithReuseIdentifier:KIDOLeadingPageCellId];
    }
    
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.numberOfPages;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IDOLeadingPageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KIDOLeadingPageCellId forIndexPath:indexPath];
    
    if (indexPath.row == self.numberOfPages - 1) {
        // 最后一页
        cell.finishButton.hidden = NO;
        
        // 完成按钮点击
        if (self.finishHandler) {
            __weak typeof(self) __weakSelf = self;
            cell.finishButtonClickBlock = ^(UIButton *finishButton) {
                __weakSelf.finishHandler(__weakSelf, finishButton, indexPath);
            };
        }
    
    } else {
        // 非最后一页
        cell.finishButton.hidden = YES;
    }
    
    // 设置数据
    if (self.setupCellHandler) {
        self.setupCellHandler(cell, indexPath);
    }
    
    return cell;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger currentPage = (scrollView.contentOffset.x / scrollView.bounds.size.width) + 0.5; // 向下取整
    if (self.pageControl.currentPage != currentPage) {
        // 小白点变化
        self.pageControl.currentPage = currentPage;
    }
}


#pragma mark - getter
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 滑动方向
        flowLayout.itemSize = self.view.bounds.size; // 元素大小
        flowLayout.minimumLineSpacing = 0.f; // 行间距
        flowLayout.minimumInteritemSpacing = 0.f; // 列间距
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.numberOfPages = self.numberOfPages;
        pageControl.currentPage = 0;
        
        // frame设置
        CGSize pageControlSize = [pageControl sizeForNumberOfPages:self.numberOfPages];
        CGFloat pageControlX = (self.view.bounds.size.width - pageControlSize.width) / 2.f;
        CGFloat pageControlY = (self.view.bounds.size.height - pageControlSize.height - 50.f);
        pageControl.frame = CGRectMake(pageControlX, pageControlY, pageControlSize.width, pageControlSize.height);
        
        _pageControl = pageControl;
    }
    return _pageControl;
}

@end
